package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.ISchoolController;
import zxq.rollcall.course.entity.SystemSchoolTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.set.SchoolDTO;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.entity.query.SchoolQuery;
import zxq.rollcall.course.service.MPSystemSchoolTableService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学校表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/school")
@Api(tags ="系统学校表")
public class SystemSchoolTableAction implements ISchoolController {

    @Autowired
    MPSystemSchoolTableService schoolTableService;

    @Override
    public ResultDTO<Boolean> save(SchoolQuery schoolQuery) {
        SystemSchoolTableEntity systemSchoolTableEntity = new SystemSchoolTableEntity();
        BeanUtils.copyProperties(schoolQuery,systemSchoolTableEntity);
        Boolean rst = schoolTableService.save(systemSchoolTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"插入成功",true);
        }

        return new ResultDTO<>(0,"插入失败",false);
    }


    @Override
    public ResultDTO<Boolean> update(Integer id, SchoolQuery schoolQuery) {
        SystemSchoolTableEntity systemSchoolTableEntity = new SystemSchoolTableEntity();
        BeanUtils.copyProperties(schoolQuery,systemSchoolTableEntity);
        systemSchoolTableEntity.setId(id);
        Boolean rst = schoolTableService.updateById(systemSchoolTableEntity);
        if(rst) {
            return new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }


    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        Boolean rst =  schoolTableService.removeById(id);
        if(rst) {
            return  new ResultDTO<>(1,"删除失败",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }


    @Override
    public ResultDTO<List<SchoolDTO>> query(PageQuery pageQuery) {
        Page<SystemSchoolTableEntity> page = new Page(pageQuery.getPage(),pageQuery.getCount());
        List<SchoolDTO> schoolDTOS =   schoolTableService.page(page).getRecords().stream().map(item -> {
            SchoolDTO schoolDTO = new SchoolDTO();
            BeanUtils.copyProperties(item,schoolDTO);
            return schoolDTO;
        }).collect(Collectors.toList());
        return new ResultDTO<>(1,"查询成功",schoolDTOS);
    }

}

